﻿using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Linq;
using System.Web;

namespace Developer.Service
{
    public class ValidCodeService
    {
        private static ConcurrentDictionary<string, Tuple<string, DateTime>> Codes = new ConcurrentDictionary<string, Tuple<string, DateTime>>();
        private static ConcurrentDictionary<string, Tuple<string, DateTime>> PwdCode = new ConcurrentDictionary<string, Tuple<string, DateTime>>();
        /// <summary>
        /// 设置手机号码及对应的验证码信息
        /// </summary>
        /// <param name="phoneNumber">手机号码</param>
        /// <param name="code">对应的验证码</param>
        public static void SetValidCode(string phoneNumber, string code)
        {
            Codes.TryAdd(phoneNumber, new Tuple<string, DateTime>(code, DateTime.Now));
        }
        /// <summary>
        /// 验证用户的验证码是否有效，验证码有效期5分钟
        /// </summary>
        /// <param name="phoneNumber">手机号</param>
        /// <param name="code">验证码</param>
        /// <returns>
        /// 验证反馈状态，0:通过验证，验证码可继续使用；1.表示验证码超时；2.表示验证码错误；3.手机号码未注册
        /// </returns>
        public static int ValidCode(string phoneNumber, string code)
        {
            if (!Codes.ContainsKey(phoneNumber))
            {
                return 3;
            }
            Tuple<string, DateTime> codeItem;
            if (Codes.TryGetValue(phoneNumber, out codeItem))
            {
                if (codeItem.Item1 != code)
                {
                    return 2;
                }
                // 验证时间是否在5分钟以内
                else if (codeItem.Item2.AddMinutes(3) < DateTime.Now)
                {
                    return 1;
                }
                else
                {
                    //删除验证码
                    Codes.TryRemove(phoneNumber, out codeItem);
                    return 0;
                }
            }
            else
            {
                return 1;
            }
        }



        /// <summary>
        /// 设置手机号码及对应的验证码信息（ResetPassword）
        /// </summary>
        /// <param name="phoneNumber">手机号码</param>
        /// <param name="code">对应的验证码</param>
        public static void ResetPwdSetValidCode(string phoneNumber, string code)
        {
            PwdCode.TryAdd(phoneNumber, new Tuple<string, DateTime>(code, DateTime.Now));
        }
        /// <summary>
        /// 验证用户的验证码是否有效，验证码有效期5分钟（ResetPassword）
        /// </summary>
        /// <param name="phoneNumber">手机号</param>
        /// <param name="code">验证码</param>
        /// <returns>
        /// 验证反馈状态，0:通过验证，验证码可继续使用；1.表示验证码超时；2.表示验证码错误；3.手机号码未注册
        /// </returns>
        public static int ResetPwdValidCode(string phoneNumber, string code)
        {
            if (!PwdCode.ContainsKey(phoneNumber))
            {
                return 3;
            }
            Tuple<string, DateTime> codeItem;
            if (PwdCode.TryGetValue(phoneNumber, out codeItem))
            {
                if (codeItem.Item1 != code)
                {
                    return 2;
                }
                // 验证时间是否在5分钟以内
                else if (codeItem.Item2.AddMinutes(3) < DateTime.Now)
                {
                    return 1;
                }
                else
                {
                    //删除验证码
                    PwdCode.TryRemove(phoneNumber, out codeItem);
                    return 0;
                }
            }
            else
            {
                return 1;
            }
        }
    }
}